package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class GroupPeer extends BasePeer{
	public static final String tableName = "Group";
	public static final String GROUPID = "groupId";
	public static final String NAME = "name";
	public static final String DESCRIPTION = "description";
	public static final String PUBLICURL = "publicURL";
	public static final String UPDATECOUNT = "updateCount";
	public static final String TOTALDOWNLOAD = "totalDownload";
	public static final String DOWNLOADPERWEEK = "downloadPerWeek";
	public static final String DOWNLOADPERDAY = "downloadPerDay";
	public static final String SKIM = "skim";
	public static final String LISTMEMBER = "listMember";
	public static final String PARTICIPATE = "participate";
	public static final String POST = "post";
	public static final String TRANSFER = "transfer";
	public static final String INVITE = "invite";
	public static final String AUDITING = "auditing";
	public static final String[] columnNames=new String[]{"groupId","name","description","publicURL","updateCount","totalDownload","downloadPerWeek","downloadPerDay","skim","listMember","participate","post","transfer","invite","auditing"};
	public static Group getGroupByPKs(Object groupId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(GroupPeer.tableName,GroupPeer.columnNames," TRUE  AND `groupId`=? ",new Object[]{groupId,},0, 1,conn);
			Group group = new Group();
		fill(rs,group);
		return group;
	}
	public static Group getGroupReferenceColumnsOnlyByPKs(Object groupId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(GroupPeer.tableName,new String[]{"groupId",}," TRUE AND `groupId`=?",new Object[]{groupId,}, 0,  1,conn);
			Group group = new Group();
		fill(rs,group);
		return group;
	}
	public static Group getGroupByGroupId(long groupId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(GroupPeer.tableName,GroupPeer.columnNames,"`groupId`=?",new Object[]{groupId},0, 1,conn);
			Group group = new Group();
		fill(rs,group);
		return group;
	}
	public static void fill(ResultSet rs, Group group) throws SQLException {
		if(rs.next()){
			try{group.setGroupId(rs.getLong("groupId"));}catch(Exception ex){}
			try{group.setName(rs.getString("name"));}catch(Exception ex){}
			try{group.setDescription(rs.getString("description"));}catch(Exception ex){}
			try{group.setPublicURL(rs.getString("publicURL"));}catch(Exception ex){}
			try{group.setUpdateCount(rs.getLong("updateCount"));}catch(Exception ex){}
			try{group.setTotalDownload(rs.getInt("totalDownload"));}catch(Exception ex){}
			try{group.setDownloadPerWeek(rs.getInt("downloadPerWeek"));}catch(Exception ex){}
			try{group.setDownloadPerDay(rs.getInt("downloadPerDay"));}catch(Exception ex){}
			try{group.setSkim(rs.getInt("skim"));}catch(Exception ex){}
			try{group.setListMember(rs.getInt("listMember"));}catch(Exception ex){}
			try{group.setParticipate(rs.getInt("participate"));}catch(Exception ex){}
			try{group.setPost(rs.getInt("post"));}catch(Exception ex){}
			try{group.setTransfer(rs.getInt("transfer"));}catch(Exception ex){}
			try{group.setInvite(rs.getInt("invite"));}catch(Exception ex){}
			try{group.setAuditing(rs.getInt("auditing"));}catch(Exception ex){}
			group.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Group> groupCollection) throws SQLException {
		while(rs.next()){
			Group group=new Group();
			try{group.setGroupId(rs.getLong("groupId"));}catch(Exception ex){}
			try{group.setName(rs.getString("name"));}catch(Exception ex){}
			try{group.setDescription(rs.getString("description"));}catch(Exception ex){}
			try{group.setPublicURL(rs.getString("publicURL"));}catch(Exception ex){}
			try{group.setUpdateCount(rs.getLong("updateCount"));}catch(Exception ex){}
			try{group.setTotalDownload(rs.getInt("totalDownload"));}catch(Exception ex){}
			try{group.setDownloadPerWeek(rs.getInt("downloadPerWeek"));}catch(Exception ex){}
			try{group.setDownloadPerDay(rs.getInt("downloadPerDay"));}catch(Exception ex){}
			try{group.setSkim(rs.getInt("skim"));}catch(Exception ex){}
			try{group.setListMember(rs.getInt("listMember"));}catch(Exception ex){}
			try{group.setParticipate(rs.getInt("participate"));}catch(Exception ex){}
			try{group.setPost(rs.getInt("post"));}catch(Exception ex){}
			try{group.setTransfer(rs.getInt("transfer"));}catch(Exception ex){}
			try{group.setInvite(rs.getInt("invite"));}catch(Exception ex){}
			try{group.setAuditing(rs.getInt("auditing"));}catch(Exception ex){}
			group.resetModified();
			groupCollection.add(group);
		}
	}
	public static boolean update(Group group, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < GroupPeer.columnNames.length; i++) {
			if (((GroupBase)group).modified[i]) {
				columns.add(GroupPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Group` SET ");
		sql.append("WHERE TRUE AND `groupId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,group.get(column));
		}
		pstmt.setObject(columns.size()+1+0,group.get("groupId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Group group, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (group.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Group` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (group.get(column) != null) {
				pstmt.setObject(i + 1 + j, group.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Group> getGroupCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Group> collection=new Vector<Group>();
		fill(rs,collection);
		return collection;
	}
}
